<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Message extends Controller
{
    /**
     * @param Request $request
     * @return \think\response\Json
     * 留言列表
     */
    public function index(Request $request)
    {
        $limit      = $request->param('limit') ?: 10;
        $page       = $request->param('page') ?: 1;
        $start_time = $request->param('start_time') ?: '';
        $end_time   = $request->param('end_time') ?: '';
        $keyword    = $request->param('keyword') ?: '';
        $type       = $request->param('type') ?: 1;//1新留言  2意向客户 3已处理

        if ($start_time && $end_time) {
            $time1                = strtotime($start_time);
            $time2                = strtotime($end_time);
            $where['creati_time'] = ['between', "$time1,$time2"];
        }

        if ($keyword) {
            $where['content|name|tel'] = ['like', "%{$keyword}%"];
        }

        if ($type) {
            $where['type'] = $type;
        }

        $message_list = Db::table('ad_message')->where($where)->order(['creati_time' => 'desc'])->paginate($limit,
            false,
            [
                'page' => $page,
                'query' => [
                    'keyword' => $keyword,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'type' => $type
                ]
            ])->each(function ($item, $key) {
            $user_info             = Db::table('ad_user_info')->where(['uid' => $item['uid']])->field([
                'job_two',
                'province',
                'city'
            ])->find();
            $item['province_name'] = Db::table('ad_provinces')->where(['provinceid' => $user_info['province']])->value('province');
            $item['city_name']     = Db::table('ad_cities')->where(['cityid' => $user_info['city']])->value('city');
            $item['trade_name']    = Db::table('ad_industry')->where(['id' => $user_info['job_two']])->value('name');
            return $item;
        });
        dd($message_list);
        return json(['code' => 200, 'info' => 'success', 'data' => $message_list]);
    }



}